<template>
  <div class="app-container">
    
    <!-- 拆货对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="捆包号" prop="bundleNumber">
          <el-input v-model="form.bundleNumber" placeholder="请输入捆包号" disabled />
        </el-form-item>
        <el-form-item prop="numberOfSplit">
          <span slot="label">
            <el-tooltip content="输入件数后，点回车，自动计算重量" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
            拆出件数
          </span>
          <el-input v-model="form.numberOfSplit" placeholder="请输入件数" clearable @keyup.enter.native="handleSplitWeight" />
        </el-form-item>
        <el-form-item label="拆出重量" prop="splitWeight">
          <el-input v-model="form.splitWeight" placeholder="请输入重量" clearable/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { splitGood } from "@/api/goods/good";
import { isNull } from "@/utils/validate";

export default {
  name: "Split",
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      //货物件数
      numberOfPiece: 0,
      //货物重量
      // splitWeight: 0,
      //选中行
      goodsRow: [],

      // 表单校验
      rules: {
      }
    };
  },
  created() {
  
  },
  methods: {
    init(open, title, numberOfPiece, goodsRow) {
      this.open = open;
      this.title = title;
      this.numberOfPiece = numberOfPiece;
      this.goodsRow = goodsRow;
      //console.log("this.goodsRow", this.goodsRow);

      this.$set(this.form, 'bundleNumber', this.goodsRow.bundleNumber);
      this.$set(this.form, 'numberOfSplit', this.goodsRow.numberOfPiece);
      //this.$set(this.form, 'splitWeight', this.goodsRow.weight);
      this.$set(this.form, 'splitWeight', '');

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bundleNumber: null,
        numberOfSplit: null,
        splitWeight: null,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function() {
      var splitWeight;
      if(isNull(this.form.splitWeight)) {
        splitWeight = 0;
      } else {
        splitWeight = this.form.splitWeight;
      }
      //const goodsRow = this.goodsRow;
      if (this.form.numberOfSplit <= 0) {
        this.$modal.msgError("拆分件数不能小于或等于零");
        return;
      } else if (this.form.numberOfSplit > this.numberOfPiece) {
        this.$modal.msgError("拆分件数不能大于原件数");
        return;
      } else if(this.form.splitWeight <= 0) {
        this.$modal.msgError("拆分重量不能小于或等于零");
        return;
      } /*else if(this.form.splitWeight > this.goodsRow.weight) {
        this.$modal.msgError("拆分重量不能大于原重量");
        return;
      }*/ else {
        this.$refs["form"].validate(valid => {
          if (valid) {
            //console.log('this.form', this.form);
            splitGood(this.form.numberOfSplit, splitWeight, this.goodsRow).then(response => {
              this.$modal.msgSuccess("拆分成功");
              this.open = false;
              this.$parent.getList();
            }).catch((e) => {
              console.log('e', e);
              this.open = false;
              this.$parent.getList();
              //this.$modal.msgError(response.msg);
            });
          }
        });
      }
    },
    handleSplitWeight() {
      console.log("this.form.numberOfSplit", this.form.numberOfSplit);
      var splitWeight
      if (this.form.numberOfSplit <= 0) {
        this.$modal.msgError("拆分件数不能小于或等于零");
        return;
      } else if (this.form.numberOfSplit > this.numberOfPiece) {
        this.$modal.msgError("拆分件数不能大于或等于原件数");
        return;
      } else {
        splitWeight = this.goodsRow.weight / this.goodsRow.numberOfPiece * this.form.numberOfSplit;
        this.form.splitWeight = splitWeight.toFixed(3);
        console.log("this.form.splitWeight", this.form.splitWeight);
      }      
    }
  }
};
</script>
